<?php
    session_start();
    error_reporting(0);
    // 建立连接
    include('sql.php');
    if (!$link) {
        die('连接失败：' . mysqli_connect_error());
    }
    $number = $_POST['number'];
    $password = md5($_POST['password']);
    $query = "SELECT * FROM user WHERE number='$number' AND password='$password'";
    $result = mysqli_query($link, $query);
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $id = $row['id'];
            $_SESSION['id'] = $id;
            $uid = $id;
            $time = time();
            $ip = $_SERVER["REMOTE_ADDR"];
            $log = "insert into user_log (uid,ip,time) values ('$uid','$ip','$time')";
            $log_result = $link->query($log);
            // 返回成功的JSON响应
            header('Content-Type: application/json');
            echo json_encode(array('success' => true, 'message' => '登录成功，<a href="home.php">点击跳转</a>', 'redirect' => 'home.php'));
            exit;
        }
    } else {
        // 返回错误的JSON响应
        header('Content-Type: application/json');
        echo json_encode(array('success' => false, 'message' => '用户名或密码错误'));
        exit;
    }
    mysqli_close($link);
?>